package com.qiqijuice.sales;

import net.miginfocom.swt.MigLayout;

import org.eclipse.swt.SWT;
import org.eclipse.swt.events.SelectionAdapter;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Control;
import org.eclipse.swt.widgets.Group;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.Text;

import com.db4o.ObjectContainer;

public class ProductCRUDView extends Composite {

	Text  txtName;
	Text  txtPrice;
	Text  txtDesc;
	Product curProduct;
	
	public ProductCRUDView(Composite parent, int style) {
		super(parent, style);
		createView();
	}
	
	void createView() {
		
		MigLayout layout = new MigLayout("", "[][][]40[right]", "[][][]30[]");
		
		setLayout(layout);
		
		Group g = new Group(this, SWT.SHADOW_IN);
		g.setLayoutData("span 3 3, grow");
		g.setLayout(new MigLayout("wrap 2", "[right]20[grow,fill]"));
		g.setText("Product Information:");
		
		Label l = new Label(g, SWT.RIGHT );
		l.setText("Product Name:");
		//l.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true, true));
		
		Text t = new Text(g, SWT.SINGLE | SWT.BORDER);
		//t.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true, true));
		txtName = t;
		t.setLayoutData("");
		
		l = new Label(g, 0);
		l.setText("Price:");
		//l.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true, true));
		
		t = new Text(g, SWT.SINGLE | SWT.BORDER);
		//t.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true, true));
		txtPrice = t;
		//t.setLayoutData("");
		
		l = new Label(g, 0);
		l.setText("Description:");
		//l.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true, true));
		
		t = new Text(g, SWT.MULTI | SWT.BORDER);
		//t.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true, true));
		txtDesc = t;
		//t.setLayoutData("");

		Button b = new Button(this, SWT.PUSH);
		b.setText(">>");
		b.setLayoutData("wrap, wmin button, sg 1");
		
		b = new Button(this, SWT.PUSH);
		b.setText("<<");
		//b.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true, true));
		b.setLayoutData("wrap, sg 1");
		
		b = new Button(this, SWT.PUSH);
		b.setText("Save");
		b.setLayoutData("cell 0 3, width 90::, sg 3");
		
		b.addSelectionListener(new SelectionAdapter() {
			
			@Override
			public void widgetSelected(SelectionEvent e) {
				final String name = txtName.getText();
				final String desc = txtDesc.getText();
				final float price = Float.parseFloat(txtPrice.getText());
				
				if (curProduct != null && 
						validateConstraint(new Product(name, desc, price, curProduct.getCategory()))) {
					curProduct.setName(name);
					curProduct.setDesc(desc);
					curProduct.setPrice(price);
					ObjectContainer oc = Db4oContainerFactory.getDB();
					oc.store(curProduct);
					AppFrame.getInst().getProductView().refreshProductData();
				}
			}
		});

		b = new Button(this, SWT.PUSH);
		b.setText("Delete");
		b.setLayoutData("cell 1 3, sg 3");
		
		viewProduct(null);
		pack();
	}

	public boolean validateConstraint(Product p) {
		return true;
	}
	
	public void save(Product p) {
		ObjectContainer oc = Db4oContainerFactory.getDB();
		oc.store(p);
		oc.commit();
	}

	public void viewProduct(Product p) {
		boolean enabled = true;
		if (p != null) {
			txtName.setText(p.getName());
			txtDesc.setText(p.getDesc());
			txtPrice.setText(""+p.getPrice());
		}
		else {
			txtName.setText("");
			txtDesc.setText("");
			txtPrice.setText("");
			enabled = false;
		}
		
		curProduct = p;
		
		for (Control c : getChildren()) {
			c.setEnabled(enabled);
		}
		txtName.setEnabled(enabled);
		txtDesc.setEnabled(enabled);
		txtPrice.setEnabled(enabled);
	}
}
